<?php

namespace App\Models;

use App\Http\Controllers\Admin\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 数字资源书单推荐
 */
class ResourceReadingList extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_reading_list';





    /*关联书单书籍*/
    public function conReadingListBook()
    {
        return $this->belongsToMany(ResourceTotal::class, ResourceReadingListBook::class, 'reading_id', 'resource_id');
    }

    /**
     *获取书单名称
     */
    public static function getReadingListName($reading_id)
    {
        return self::where('id', $reading_id)->where('is_del', 1)->value('name');
    }


    /**
     *获取推荐的书单
     */
    public function getRecomReadingList($limit = 3)
    {
        $res = $this->select('id', 'name', 'img', 'intro', 'create_time')
            //with 添加 limit 参数 数据有问题
            // ->with(['conReadingListBook' => function ($query) {
            //     $query->select('resource_total.id', 'resource_total.guid', 'resource_total.title', 'author', 'press', 'pre_time', 'isbn', 'img', 'intro')->where('resource_total.is_del', 1);
            // }])
            ->whereHas('conReadingListBook', function ($query) {
                $query->where('resource_total.is_del', 1); //删除掉不存在的书籍数据，同时 如果一本都不存在，则不返回当前书单
            })
            ->where('is_del', 1)
            ->where('is_play', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();
        return $res;
    }

    /**
     *获取列表
     * @param limit int 分页大小
     * @param is_play string 是否发布  1 发布 2 不发布
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $keywords = $search_data['keywords'] ?? '';
        $is_play = $search_data['is_play'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $manage_resource_business_ids = request()->manage_info['resource_business_ids'] ?? [];

        $res = $this->select('id', 'img', 'name', 'is_play',  'intro', 'create_time')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('name', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($is_play) {
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })
            ->where(function ($query) use ($manage_resource_business_ids) {
                if ($manage_resource_business_ids) {
                    $query->where('manage_id', request()->manage_id);
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }
    /**
     * 详情
     * @param id
     * @param is_play int 发布   1 发布  2 撤销
     */
    public function detail($id, $is_play = null)
    {
        $res = $this->select('id', 'img', 'name', 'is_play', 'intro', 'create_time')
            ->where('id', $id)
            ->where(function ($query) use ($is_play) {
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })
            ->where('is_del', 1)
            ->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }
}
